package com.example.hyy_xiangji2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MyCameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener{
    private SurfaceView mSurfaceView;
	private Button paiZhao;
	private Camera mCamera;
	private SurfaceHolder mHolder;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_mycamera);
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
		mSurfaceView.setOnClickListener(this);
		imageView = (ImageView)findViewById(R.id.iamgeview);
		imageView.setOnClickListener(this);
		paiZhao = (Button) findViewById(R.id.pai_zhao);
		
		mHolder = mSurfaceView.getHolder();
		mHolder.addCallback(this);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getCamera();
		setStartPreView(mCamera, mHolder);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		realseCamera();
		
	}
	
	
	public Camera getCamera(){
		if(mCamera == null){
			try {
				mCamera = Camera.open();
			} catch (Exception e) {
				mCamera = null;//*
			}
		}
		return mCamera;
	}
	
	public void setStartPreView(Camera camera,SurfaceHolder holder){
		try {
			if(camera == null){
				camera = getCamera();
			}
			if(holder == null){
				holder = mSurfaceView.getHolder();
			}
			camera.setPreviewDisplay(holder);
			camera.setDisplayOrientation(90);
			camera.startPreview();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void realseCamera(){
		if(mCamera != null){
			mCamera.setPreviewCallback(null);
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
//		TODO   顺序****
		setStartPreView(mCamera, mHolder);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		mCamera.stopPreview();
		setStartPreView(mCamera, mHolder);		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		realseCamera();
	}
	
	
	public void paiZhao(View view){
		Parameters parames = mCamera.getParameters();
		parames.setPictureFormat(ImageFormat.JPEG);
		parames.setPreviewSize(800, 400);//相机的默认是竖着的，所以设置的宽高和想象中的是相反的
		parames.setFocusMode(Parameters.FOCUS_MODE_AUTO);
//		需要一个回调，让它自动对焦完成后再拍照
		mCamera.autoFocus(new AutoFocusCallback() {
			@Override
			public void onAutoFocus(boolean success, Camera camera) {
				if(success){
//					在拍照以后，需要一个回调得到照片的真实信息
					mCamera.takePicture(null, null, mPictureCallBack);
				}
			}
		});
		
	}
	
//	接口回调，得到照片后的处理
	private File picFile;
	private PictureCallback mPictureCallBack = new PictureCallback() {

		@Override//照片的真实信息
		public void onPictureTaken(byte[] data, Camera camera) {
			 picFile = new File("/sdcard/hyy_camera.png");
			try {
				FileOutputStream fos = new FileOutputStream(picFile);
				fos.write(data);
				fos.close();
				showPic();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	};
	
//	显示拍照后的图片
	public void showPic(){
		mSurfaceView.setVisibility(View.GONE);
		imageView.setVisibility(View.VISIBLE);
//		能显示，但是图片是竖着的。  显示预览的时候只是改变了你看的方向，但实际的方向并未改变
//		Bitmap bitmap = BitmapFactory.decodeFile(picFile.getAbsolutePath());
		FileInputStream fis;
		try {
			fis = new FileInputStream(picFile.getAbsolutePath());
			Bitmap bitmap = BitmapFactory.decodeStream(fis);
//			Bitmap bitmap = BitmapFactory.decodeFile(picFile.getAbsolutePath());
//			矩阵  旋转90
			Matrix matrix = new Matrix();
			matrix.setRotate(90);
//			重新生成一个bitmap   TODO  图像处理  和 流
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
			imageView.setImageBitmap(bitmap);
			paiZhao.setClickable(false);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iamgeview:
			mSurfaceView.setVisibility(View.VISIBLE);
			paiZhao.setClickable(true);
			break;
		case R.id.surface_view:
//			设置点击事件，null：因为不需要对焦后的操作。
			mCamera.autoFocus(null);
			Toast.makeText(getApplicationContext(), "对焦", 0).show();
			break;

		default:
			break;
		}
	}
	
	
}
